-module(pb_account).

-export([pack/2, pack_msg/2, unpack/2, unpack_msg/2]).


%% 心跳
pack(2000, {}) ->
	BinData = <<>>,
	{ok, pb:msg(2000, BinData)};

%% 账号登录
pack(2010, {Account,Time,Sign}) ->
	Bin1 = pb:encode(string, Account),
	Bin2 = pb:encode(u32, Time),
	Bin3 = pb:encode(string, Sign),
	BinData = <<Bin1/binary,Bin2/binary,Bin3/binary>>,
	{ok, pb:msg(2010, BinData)};

%% 账号登录成功
pack(2020, {}) ->
	BinData = <<>>,
	{ok, pb:msg(2020, BinData)};

%% 获取账号角色列表
pack(2030, {Sid}) ->
	Bin1 = pb:encode(u16, Sid),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(2030, BinData)};

%% 账号角色列表返回
pack(2040, {Roles}) ->
	FunRoles = fun(FRoles, {CountAcc, BinAcc}) ->
			FBin = pb_role:pack_msg(3900, FRoles),
			{CountAcc + 1, <<BinAcc/binary, FBin/binary>>}
	end,
	{CountRoles, BinRoles} = lists:foldl(FunRoles, {0, <<>>}, Roles),
	Bin1 = pb:encode(u16, CountRoles),
	Bin2 = BinRoles,
	BinData = <<Bin1/binary,Bin2/binary>>,
	{ok, pb:msg(2040, BinData)};

%% 随机名字
pack(2050, {Sid,Sex}) ->
	Bin1 = pb:encode(u16, Sid),
	Bin2 = pb:encode(u8, Sex),
	BinData = <<Bin1/binary,Bin2/binary>>,
	{ok, pb:msg(2050, BinData)};

%% 随机名字返回
pack(2060, {Name}) ->
	Bin1 = pb:encode(string, Name),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(2060, BinData)};

%% 创建角色
pack(2070, {Sid,Sex,Name}) ->
	Bin1 = pb:encode(u16, Sid),
	Bin2 = pb:encode(u8, Sex),
	Bin3 = pb:encode(string, Name),
	BinData = <<Bin1/binary,Bin2/binary,Bin3/binary>>,
	{ok, pb:msg(2070, BinData)};

%% 创建角色成功
pack(2080, {Rid}) ->
	Bin1 = pb:encode(u64, Rid),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(2080, BinData)};

%% 角色登录
pack(2090, {Rid,ExtParam}) ->
	Bin1 = pb:encode(u64, Rid),
	Bin2 = pb:encode(string, ExtParam),
	BinData = <<Bin1/binary,Bin2/binary>>,
	{ok, pb:msg(2090, BinData)};

pack(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 心跳
pack_msg(2000, {}) ->
	<<>>;

%% 账号登录
pack_msg(2010, {Account,Time,Sign}) ->
	Bin1 = pb:encode(string, Account),
	Bin2 = pb:encode(u32, Time),
	Bin3 = pb:encode(string, Sign),
	<<Bin1/binary,Bin2/binary,Bin3/binary>>;

%% 账号登录成功
pack_msg(2020, {}) ->
	<<>>;

%% 获取账号角色列表
pack_msg(2030, {Sid}) ->
	Bin1 = pb:encode(u16, Sid),
	<<Bin1/binary>>;

%% 账号角色列表返回
pack_msg(2040, {Roles}) ->
	FunRoles = fun(FRoles, {CountAcc, BinAcc}) ->
			FBin = pb_role:pack_msg(3900, FRoles),
			{CountAcc + 1, <<BinAcc/binary, FBin/binary>>}
	end,
	{CountRoles, BinRoles} = lists:foldl(FunRoles, {0, <<>>}, Roles),
	Bin1 = pb:encode(u16, CountRoles),
	Bin2 = BinRoles,
	<<Bin1/binary,Bin2/binary>>;

%% 随机名字
pack_msg(2050, {Sid,Sex}) ->
	Bin1 = pb:encode(u16, Sid),
	Bin2 = pb:encode(u8, Sex),
	<<Bin1/binary,Bin2/binary>>;

%% 随机名字返回
pack_msg(2060, {Name}) ->
	Bin1 = pb:encode(string, Name),
	<<Bin1/binary>>;

%% 创建角色
pack_msg(2070, {Sid,Sex,Name}) ->
	Bin1 = pb:encode(u16, Sid),
	Bin2 = pb:encode(u8, Sex),
	Bin3 = pb:encode(string, Name),
	<<Bin1/binary,Bin2/binary,Bin3/binary>>;

%% 创建角色成功
pack_msg(2080, {Rid}) ->
	Bin1 = pb:encode(u64, Rid),
	<<Bin1/binary>>;

%% 角色登录
pack_msg(2090, {Rid,ExtParam}) ->
	Bin1 = pb:encode(u64, Rid),
	Bin2 = pb:encode(string, ExtParam),
	<<Bin1/binary,Bin2/binary>>;

pack_msg(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 心跳
unpack(2000, _Bin0) ->
	{ok, {}};

%% 账号登录
unpack(2010, _Bin0) ->
	{Account, _Bin1} = pb:decode(string, _Bin0),
	{Time, _Bin2} = pb:decode(u32, _Bin1),
	{Sign, _Bin3} = pb:decode(string, _Bin2),
	{ok, {Account,Time,Sign}};

%% 账号登录成功
unpack(2020, _Bin0) ->
	{ok, {}};

%% 获取账号角色列表
unpack(2030, _Bin0) ->
	{Sid, _Bin1} = pb:decode(u16, _Bin0),
	{ok, {Sid}};

%% 账号角色列表返回
unpack(2040, _Bin0) ->
	{RolesCount, _Bin1} = pb:decode(u16, _Bin0),
	FunRoles = fun(_, {RolesAcc, _BinRolesAcc}) ->
				{FunRoles, _BinRolesAcc2} = pb_role:unpack_msg(3900, _BinRolesAcc),
				{[FunRoles | RolesAcc], _BinRolesAcc2}
			end,
	{RolesTmp, _Bin2} = lists:foldl(FunRoles, {[], _Bin1}, lists:duplicate(RolesCount, 0)),
	Roles = lists:reverse(RolesTmp),
	{ok, {Roles}};

%% 随机名字
unpack(2050, _Bin0) ->
	{Sid, _Bin1} = pb:decode(u16, _Bin0),
	{Sex, _Bin2} = pb:decode(u8, _Bin1),
	{ok, {Sid,Sex}};

%% 随机名字返回
unpack(2060, _Bin0) ->
	{Name, _Bin1} = pb:decode(string, _Bin0),
	{ok, {Name}};

%% 创建角色
unpack(2070, _Bin0) ->
	{Sid, _Bin1} = pb:decode(u16, _Bin0),
	{Sex, _Bin2} = pb:decode(u8, _Bin1),
	{Name, _Bin3} = pb:decode(string, _Bin2),
	{ok, {Sid,Sex,Name}};

%% 创建角色成功
unpack(2080, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{ok, {Rid}};

%% 角色登录
unpack(2090, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{ExtParam, _Bin2} = pb:decode(string, _Bin1),
	{ok, {Rid,ExtParam}};

unpack(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.

%% 心跳
unpack_msg(2000, _Bin0) ->
	{{},_Bin0};

%% 账号登录
unpack_msg(2010, _Bin0) ->
	{Account, _Bin1} = pb:decode(string, _Bin0),
	{Time, _Bin2} = pb:decode(u32, _Bin1),
	{Sign, _Bin3} = pb:decode(string, _Bin2),
	{{Account,Time,Sign},_Bin3};

%% 账号登录成功
unpack_msg(2020, _Bin0) ->
	{{},_Bin0};

%% 获取账号角色列表
unpack_msg(2030, _Bin0) ->
	{Sid, _Bin1} = pb:decode(u16, _Bin0),
	{{Sid},_Bin1};

%% 账号角色列表返回
unpack_msg(2040, _Bin0) ->
	{RolesCount, _Bin1} = pb:decode(u16, _Bin0),
	FunRoles = fun(_, {RolesAcc, _BinRolesAcc}) ->
				{FunRoles, _BinRolesAcc2} = pb_role:unpack_msg(3900, _BinRolesAcc),
				{[FunRoles | RolesAcc], _BinRolesAcc2}
			end,
	{RolesTmp, _Bin2} = lists:foldl(FunRoles, {[], _Bin1}, lists:duplicate(RolesCount, 0)),
	Roles = lists:reverse(RolesTmp),
	{{Roles},_Bin2};

%% 随机名字
unpack_msg(2050, _Bin0) ->
	{Sid, _Bin1} = pb:decode(u16, _Bin0),
	{Sex, _Bin2} = pb:decode(u8, _Bin1),
	{{Sid,Sex},_Bin2};

%% 随机名字返回
unpack_msg(2060, _Bin0) ->
	{Name, _Bin1} = pb:decode(string, _Bin0),
	{{Name},_Bin1};

%% 创建角色
unpack_msg(2070, _Bin0) ->
	{Sid, _Bin1} = pb:decode(u16, _Bin0),
	{Sex, _Bin2} = pb:decode(u8, _Bin1),
	{Name, _Bin3} = pb:decode(string, _Bin2),
	{{Sid,Sex,Name},_Bin3};

%% 创建角色成功
unpack_msg(2080, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{{Rid},_Bin1};

%% 角色登录
unpack_msg(2090, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{ExtParam, _Bin2} = pb:decode(string, _Bin1),
	{{Rid,ExtParam},_Bin2};

unpack_msg(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.
